{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def romanToInt(self, s: str) -> int:\n",
    "        roman = {\n",
    "            'I': 1,\n",
    "            'V': 5,\n",
    "            'X': 10,\n",
    "            'L': 50,\n",
    "            'C': 100,\n",
    "            'D': 500,\n",
    "            'M': 1000\n",
    "        }\n",
    "        num = 0\n",
    "        length = len(s)\n",
    "        for i in range(length):\n",
    "            now = roman[s[i]]\n",
    "            if i + 1 < length:\n",
    "                right = roman[s[i+1]]\n",
    "            else:\n",
    "                right = 0\n",
    "            if now < right:\n",
    "                num = num - now\n",
    "            else:\n",
    "                num = num + now\n",
    "        return num                  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = Solution()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3999"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.romanToInt(\"MMMCMXCIX\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate ROMAN NUM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [None]*4000\n",
    "\n",
    "# 1:3 III\n",
    "for i in range(1,4):\n",
    "    a[i] = 'I' * i\n",
    "    a[i*10] = 'X' * i\n",
    "    a[i*100] = 'C' * i\n",
    "    a[i*1000] = 'M' * i\n",
    "\n",
    "# 4 IV\n",
    "a[4] = 'IV'\n",
    "a[40] = 'XL'\n",
    "a[400] = 'CD'\n",
    "\n",
    "# 5 V\n",
    "a[5] = 'V'\n",
    "a[50] = 'L'\n",
    "a[500] = 'D'\n",
    "\n",
    "# 6:8 VIII\n",
    "for i in range(6,10):\n",
    "    a[i] = a[5]+a[1]*(i-5)\n",
    "    a[i*10] = a[50] + a[10]*(i-5)\n",
    "    a[i*100] = a[500] + a[100]*(i-5)\n",
    "\n",
    "# 9 IV\n",
    "a[9] = 'IX'\n",
    "a[90] = 'XC'\n",
    "a[900] = 'CM' \n",
    "\n",
    "for i in range(1, 4000):\n",
    "    if a[i]:\n",
    "        continue\n",
    "    num = i\n",
    "    t_s = ''\n",
    "    # 1000\n",
    "    c = num // 1000\n",
    "    if c > 0:\n",
    "        t_s = t_s + a[c*1000]\n",
    "        num = num % 1000\n",
    "    # 100\n",
    "    c = num // 100\n",
    "    if c > 0:\n",
    "        t_s = t_s + a[c*100]\n",
    "        num = num % 100\n",
    "    # 10\n",
    "    c = num // 10\n",
    "    if c > 0:\n",
    "        t_s = t_s + a[c*10]\n",
    "        num = num % 10\n",
    "    # 1\n",
    "    if num > 0:\n",
    "        t_s = t_s + a[num]\n",
    "    # assign\n",
    "    a[i] = t_s "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py3",
   "language": "python",
   "name": "py3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
